﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		
		degreeFactory = getProperty("serviceFactory").getBean("degreeFactory");
		creditFactory = getProperty("serviceFactory").getBean("creditFactory");

		response = structNew();
		structInsert(response, "success", false, true);
		
		studentId = event.getArg("STU");
		templateIds = event.getArg("TPL");
		
		/* 审核人 */
		teacherId = sessionAdvice.getAutherUserID();
		/* 当前时间戳 */
		currentDateTime = datetimeAdvice.getCurrentDatetimeString();

		
		if ( len(studentId) AND len(templateIds) ) {
			
			transaction{
				
				sql = "DELETE FROM
							t_degree_details t
						WHERE
							t.stu_id = :studentId 
							AND
							t.gdc NOT IN (:templateIds )";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="templateIds", value=templateIds, cfsqltype="cf_sql_char", list=true );
				
				queryObj.execute( sql=sql );
				
				if( NOT listFind(templateIds,"CPD")){
					
					sql = "DELETE FROM
								t_mark_need t
							WHERE
								t.stu_id = :studentId 
								AND
								t.gdc = 'CPD' ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );
				}

				if( NOT listFind(templateIds,"CBD")){
					
					sql = "DELETE FROM
								t_mark_need t
							WHERE
								t.stu_id = :studentId 
								AND
								t.gdc = 'CBD' ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );
				}

			}
			
			/* 先刷新学生学分统计 */
			creditFactory.creditRefresh( studentId );
			
			for ( templateId in listToArray( templateIds, "," ) ) {
				
				/* 获取对应审核条件参数 */
				params = event.getArg( "TPLP-" & templateId, "0" );

				/* 初始化审查详细数据 */
				degreeFactory.initAudit( templateId, studentId, params );
				
				/* 调用组件审核 */
				degreeFactory.audit( templateId, studentId, params );
				
			}
			
			/* 学生的所有审核条件完成后, 判断机审条件是否都通过 */
			passed = degreeFactory.auditPass(  studentId );
			
			if( passed eq "1" ){
				structInsert(response, "passed", true, true);
			}else{
				structInsert(response, "passed", false, true);
			}
			
			structInsert(response, "operation", "学生 " & studentId & " 自动审核完成", true);
			structInsert(response, "success", true, true);
			
		}
		

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>